package cn.kgc.springmvcday03.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.SessionAttribute;
import org.springframework.web.bind.annotation.SessionAttributes;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * @author 课工场
 * @date 2023/10/17
 * @description    视图的转发和重定向
 *
 * forward  :  转发
 * redirect ： 重定向
 *
 * @SessionAttributes("key")  key  model 中存储值的key  将指定key的值存入session域对象
 *
 * @SessionAttribute("key")    在session域对象中获取指定key的值
 * @CookieValue("key")        在cookie对象中获取指定key的值
 */
@Controller
@RequestMapping("fr")
@SessionAttributes({"info"})
public class FRController {


    //转发实现页面跳转  main
    @RequestMapping("m1")
    public void method1(HttpServletRequest request, HttpServletResponse response) throws Exception {

        request.setAttribute("info","hello  HttpServletRequest");
        request.getRequestDispatcher("/main.jsp").forward(request,response);
    }

    //使用重定向 实现页面跳转
    @RequestMapping("m2")
    public void method2(HttpServletRequest request, HttpServletResponse response) throws Exception {

        HttpSession session = request.getSession();
        session.setAttribute("info","hello  HttpServletResponse response");
        response.sendRedirect(request.getContextPath()+"/main.jsp");
    }

    // mvc 实现转发
    @RequestMapping("m3")
    public String method3(Model model){

        model.addAttribute("inf","hello forward");
        return "forward:/main.jsp";  // 显示的声明使用转发实现页面跳转   需要返回视图名的全称

    }


    // mvc 实现重定向
    @RequestMapping("m4")
    public String method4(Model model){

        model.addAttribute("info","hello redirect");

        return "redirect:/main.jsp";  //  不经过视图解析器  需要写页面的全称

    }

    // mvc 实现重定向
    @RequestMapping("m5")
    public String method5(HttpSession session){

        session.setAttribute("info","hello redirect");
        return "redirect:/main.jsp";
    }


    // 获取session中的数据   HttpServletRequest request
    @RequestMapping("m6")
    public String method6(HttpSession session){

        String info = (String) session.getAttribute("info");
        System.out.println("info = " + info);
        return "redirect:/main.jsp";
    }


    @RequestMapping("m7")
    public String method7(@SessionAttribute("info") String info){

        System.out.println("info = " + info);

        return "redirect:/main.jsp";
    }


    @RequestMapping("m8")
    public void method8(HttpServletRequest request, HttpServletResponse response) throws Exception{

        Cookie ck1 = new Cookie("username","tom");
        Cookie ck2 = new Cookie("password","12345");

        ck1.setPath("/");
        ck2.setPath("/");

        response.addCookie(ck1);
        response.addCookie(ck2);


        response.sendRedirect("/mvc/main.jsp");
    }

    @RequestMapping("m9")
    public String method8(@CookieValue("username") String username, @CookieValue("password") String password,Model model) {

        model.addAttribute("username",username);
        model.addAttribute("password",password);

        return "main";
    }





}
